package com.hrzyit.base.widgets

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.hrzyit.base.R
import kotlinx.android.synthetic.main.layout_case_cell.view.*

/**
 * 通用案件选择单元格组件
 * @author YangWei
 * @date 2019/3/22
 */
class CommonCaseCell @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    // 左侧文本
    private var leftText: String? = null
    // 右侧文本
    private var rightText: String? = null
    // 右侧文本后图标
    private var rightDrawable: Drawable? = null

    init {

        // 初始化方法

        // 获取自定义属性 - layout_header_bar
        var typedArray = context.obtainStyledAttributes(attrs, R.styleable.CaseCell)


        leftText = typedArray.getString(R.styleable.CaseCell_leftText)

        rightText = typedArray.getString(R.styleable.CaseCell_rightText)

        rightDrawable = typedArray.getDrawable(R.styleable.CaseCell_rightDrawable)

        initView()

        // 释放属性资源
        typedArray.recycle()
    }

    /**
     * 初始化视图
     */
    private fun initView() {

        // 布局填充的的场景，使用前先实例化
        View.inflate(context, R.layout.layout_case_cell, this)

        // 左侧按钮是否显示
//        mLeftIv.visibility = if (isShowBack) View.VISIBLE else View.GONE

        // 标题文字内容
        leftText?.let {
            mLeftTv.text = it
        }

        // 右侧文字内容
        rightText?.let {
            mRightTv.text = it
        }

        // 右侧文本图标
        rightDrawable?.let {
            it.setBounds(0, 0, it.minimumWidth, it.minimumHeight)
            mRightTv.setCompoundDrawables(null, null, it, null)
            mRightTv.setPadding(20, 10, 10, 10)
        }
    }

    fun getLeftView(): TextView {
        return mLeftTv
    }

    fun getRightView(): TextView {
        return mRightTv
    }

    fun getLeftText(): String {
        return mLeftTv.text.toString()
    }

    fun getRightText(): String {
        return mRightTv.text.toString()
    }

}